//
//  SYDDownwindOrderView.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/16.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDDownwindOrderView.h"
#import "YMTravelTrack.h"

@interface SYDDownwindOrderView()
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
@property (weak, nonatomic) IBOutlet UILabel *orderTimeLbl;
@property (weak, nonatomic) IBOutlet UILabel *startLocationLbl;
@property (weak, nonatomic) IBOutlet UILabel *endStationLbl;
@property (weak, nonatomic) IBOutlet UILabel *viaLbl;//途经
@property (weak, nonatomic) IBOutlet UILabel *distanceLbl;
@property (weak, nonatomic) IBOutlet UILabel *timeLbl;
@property (weak, nonatomic) IBOutlet UILabel *numberLbl;
@property (weak, nonatomic) IBOutlet UILabel *priceLbl;
@property (weak, nonatomic) IBOutlet UILabel *userMessageLbl;
@property (weak, nonatomic) IBOutlet UIButton *agreeOrderBtn;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property(nonatomic,strong)SYDIFIManage *ifiManage;//语音
@end
@implementation SYDDownwindOrderView
#pragma mark---Lazy
-(SYDIFIManage *)ifiManage{
    if (!_ifiManage) {
        _ifiManage = [[SYDIFIManage alloc]init];
    }
    return _ifiManage;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([SYDDownwindOrderView class]) owner:nil options:nil].lastObject;
        self.frame = frame;
    }
    return self;
}



- (void)setModel:(YMOrder *)model{
    
    NSMutableAttributedString *string;
    carpoolRouteStationModel *startStation = model.carpoolRoute.carpoolRouteStations.firstObject;
    carpoolRouteStationModel *endStation = model.carpoolRoute.carpoolRouteStations.lastObject;
    
    self.orderTimeLbl.text = [NSString stringWithFormat:@"%@-%@",[NSString getHourMinuteWithString:startStation.arriveTime],[NSString getHourMinuteWithString:endStation.arriveTime]];
    
    self.startLocationLbl.text = startStation.stationName;
    self.endStationLbl.text = endStation.stationName;
    self.viaLbl.text = [NSString stringWithFormat:@"途经%ld个站点",[endStation.stationNo integerValue] - [startStation.stationNo integerValue]];
    self.distanceLbl.text = [NSString stringWithFormat:@"%.2f",[model.totalDistance floatValue]/1000];
//    if ([model.totalDistance integerValue] > 1000) {
//        self.distanceLbl.text = [NSString stringWithFormat:@"%.2f",[model.totalDistance floatValue]/1000];
//    }else{
//        self.distanceLbl.text = [model.totalDistance stringByAppendingString:@""];
//    }
    
    //NSString *str_hour = [NSString stringWithFormat:@"%02ld",[model.costTime integerValue]/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",([model.costTime integerValue]%3600)/60];
    //format of second
    //NSString *str_second = [NSString stringWithFormat:@"%02ld",[model.costTime integerValue]%60];
    self.timeLbl.text = str_minute;
//    if ([str_hour integerValue] > 1) {//小时
//        
//    }else{
//        if ([str_minute integerValue] > 1) {//分钟
//            self.timeLbl.text = [NSString stringWithFormat:@"%@分钟",str_minute];
//        }else{
//         self.timeLbl.text = [str_second stringByAppendingString:@"s"];
//        }
//    }
    
    self.numberLbl.text = [NSString stringWithFormat:@"%ld",[model.persons integerValue]];
    self.priceLbl.text = [NSString stringWithFormat:@"%.2f",[model.income doubleValue]];

    string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"距离接单位置: %.02f 公里，预计%02ld分钟到达",[model.totalDistance floatValue]/1000,([model.costTime integerValue]%3600)/60]];
    [string addAttribute:NSForegroundColorAttributeName value:GREEN_COLOR range:NSMakeRange(7, 5)];
    [string addAttribute:NSForegroundColorAttributeName value:GREEN_COLOR range:NSMakeRange(18, 2)];
    [string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(7, 5)];
    [string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(18, 2)];
    self.userMessageLbl.attributedText = string;
    
    if ([YMSaveTool boolForKey:@"switchValueChanged"]) {
        [self.ifiManage openIFlySpeechWithSpeaking:[NSString stringWithFormat:@"新订单提醒,实时,从%@到%@,从%@到%@,途经%ld个站点,乘客共%@人,预计收入车费%@元",[NSString getHourMinuteWithString:startStation.arriveTime],[NSString getHourMinuteWithString:endStation.arriveTime],startStation.stationName,endStation.stationName,[endStation.stationNo integerValue] - [startStation.stationNo integerValue],model.persons,model.income]];
    } else {
        NSLog(@"无语音播报");
    }
    
    WEAKSELF
    [self.agreeOrderBtn startWithDuration:10 running:^(UIButton *button, NSInteger totalTime, NSInteger leftTime) {
        [self.agreeOrderBtn setTitle:[NSString stringWithFormat:@"接单%.2ld s",(long)leftTime] forState:UIControlStateNormal];
        [self.agreeOrderBtn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        self.agreeOrderBtn.userInteractionEnabled = YES;
        
        [[weakSelf.closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
             [weakSelf.ifiManage closeIFlySpeech];
            if (weakSelf.block) {
                weakSelf.block();
            }
        }];
        
        [[weakSelf.agreeOrderBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
             [weakSelf.ifiManage closeIFlySpeech];
            if (weakSelf.agreeBlock) {
                weakSelf.agreeBlock();
            }
        }];
    } finishedBlock:^(UIButton *button) {
        [self.agreeOrderBtn setTitle:@"拒单了" forState:UIControlStateNormal];
        [self.agreeOrderBtn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        self.agreeOrderBtn.userInteractionEnabled = NO;
        [weakSelf.ifiManage closeIFlySpeech];
        if (weakSelf.block) {
            weakSelf.block();
        }
    }];
    
}

-(void)refreshStatusWithServer{
    
    
    
}
-(void)awakeFromNib{
    [super awakeFromNib];
    [self.agreeOrderBtn drawBorderWithColor:GREEN_COLOR radius:5];
    [self.imageView addRoundedCorners:UIRectCornerTopLeft withRadii:CGSizeMake(5, 5)];
    WEAKSELF
    [[weakSelf.closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.block) {
            weakSelf.block();
        }
    }];
    
    [[weakSelf.agreeOrderBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.agreeBlock) {
            weakSelf.agreeBlock();
        }
    }];
}

@end
